//
//  SelectTicketView.swift
//  旅航ios
//
//  Created by tulip on 2021/6/10.
//

import UIKit
import SwiftyJSON

class SelectTicketView: UIView,UITableViewDelegate,UITableViewDataSource {
    
    var propData:((_ arr1:Array<String>,_ arr2:Array<Int>,_ arr3:Array<String>)->Void)?
    var airportData:[JSON]?
    var cabinData:Array<(Int,String)>?
    var airNameData:[JSON]?
    var selectCell_1:Int=0
    var selectCell_2:Int=0
    var selectCell_3:Int=0
    
    var airPortArr:Array<String>=[]
    var cabinArr:Array<Int>=[]
    var airNameArr:Array<String>=[]
    var airPortCellArr:Array<SelectTableCell>=[]
    var cabinCellArr:Array<SelectTableCell>=[]
    var airNameCellArr:Array<SelectTableCell>=[]
    
    var tableArr:Array<UITableView>=[]
    var btnArr:Array<UIButton>=[]
    var collect:UICollectionView!
    var table1:UITableView!
    var table2:UITableView!
    var table3:UITableView!

    var box:UIView!
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        cabinData=[(1,"经济舱"),(2,"头等舱/公务舱")]
        
        self.backgroundColor=UIColor.black.withAlphaComponent(0.6)
        box=UIView.init(frame: CGRect(x: 0, y: SCREEN_HEIGHT-400, width: SCREEN_WIDTH, height: 400))
        box.backgroundColor=UIColor.white
        self.addSubview(box)
        
        
        let cancelBtn=UIButton.init(frame: CGRect(x: 10, y: 20, width: 35, height: 22))
        cancelBtn.setTitle("取消", for: .normal)
        cancelBtn.setTitleColor(UIColor.init(hexString: "666666"), for: .normal)
        cancelBtn.addTarget(self, action: #selector(cancel), for: .touchUpInside)
        cancelBtn.titleLabel?.font=UIFont.systemFont(ofSize: 13)
        box.addSubview(cancelBtn)
        
        let sureBtn=UIButton.init(frame: CGRect(x: SCREEN_WIDTH - 45, y: 20, width: 35, height: 22))
        sureBtn.setTitle("确认", for: .normal)
        sureBtn.setTitleColor(UIColor.init(hexString: "3a83f9"), for: .normal)
        sureBtn.addTarget(self, action: #selector(sure), for: .touchUpInside)
        sureBtn.titleLabel?.font=UIFont.systemFont(ofSize: 13)
        box.addSubview(sureBtn)
        
        let title=UILabel.init(frame: CGRect(x: SCREEN_WIDTH/2.0 - 50, y: 20, width: 100, height: 22))
        title.text="筛选"
        title.font=UIFont.boldSystemFont(ofSize: 17)
        title.textAlignment = .center
        title.textColor=UIColor.black
        box.addSubview(title)
        
        let line1=UIView.init(frame: CGRect(x: 0, y: 59, width: ScreenWidth, height: 1))
        line1.backgroundColor=UIColor.init(hexString: "ebebeb")
        box.addSubview(line1)
        
       // collect=UICollectionView.init(frame: CGRect(x: 0, y: 61, width: SCREEN_WIDTH, height: 60))
       // self.addSubview(collect)
        
//        let line2=UIView.init(frame: CGRect(x: 0, y: 121, width: ScreenWidth, height: 1))
//        line2.backgroundColor=UIColor.init(hexString: "ebebeb")
//        box.addSubview(line2)
        
        let btnBox=UIView.init(frame: CGRect(x: 0, y: 60, width: 95, height: box.frame.size.height-60))
        box.addSubview(btnBox)
        btnBox.backgroundColor=UIColor.init(hexString: "f5f5f5")
        
        let btnNameArr=["机场","舱位","航空公司"]
        for i in 0...2{
            let btn=UIButton.init(frame: CGRect(x: 0, y: 45*i, width: 95, height: 45))
            btn.contentHorizontalAlignment = .left
            btn.titleEdgeInsets=UIEdgeInsets(top: 0, left: 15, bottom: 0, right: 0)
            btn.setTitleColor(UIColor.init(hexString: "555555"), for: .normal)
            btn.setTitle(btnNameArr[i], for: .normal)
            btn.titleLabel?.font=UIFont.systemFont(ofSize: 14)
            btn.tag=i
            btnArr.append(btn)
            btn.addTarget(self, action: #selector(clickBtn), for: .touchUpInside)
            btnBox.addSubview(btn)
        }
        
        table1=UITableView.init(frame: CGRect(x: 95, y: 60, width: SCREEN_WIDTH-95, height: box.frame.size.height-60-BottomSpaceHeigh))
        table1.delegate=self
        table1.dataSource=self
        table1.showsVerticalScrollIndicator=false
        table1.separatorStyle = .none
        table1.register(UINib(nibName: "SelectTableCell", bundle: nil), forCellReuseIdentifier: "tablecell1")
        box.addSubview(table1)
        tableArr.append(table1)
        
        table2=UITableView.init(frame: CGRect(x: 95, y: 60, width: SCREEN_WIDTH-95, height: box.frame.size.height-60-BottomSpaceHeigh))
        table2.delegate=self
        table2.dataSource=self
        table2.showsVerticalScrollIndicator=false
        table2.separatorStyle = .none
        table2.register(UINib(nibName: "SelectTableCell", bundle: nil), forCellReuseIdentifier: "tablecell2")
        tableArr.append(table2)
        
        table3=UITableView.init(frame: CGRect(x: 95, y: 60, width: SCREEN_WIDTH-95, height: box.frame.size.height-60-BottomSpaceHeigh))
        table3.delegate=self
        table3.dataSource=self
        table3.showsVerticalScrollIndicator=false
        table3.separatorStyle = .none
        table3.register(UINib(nibName: "SelectTableCell", bundle: nil), forCellReuseIdentifier: "tablecell3")
        tableArr.append(table3)
        
       
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        45
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if tableView == table1 {
            
            return airportData?.count ?? 0
        }else if tableView == table2{
            
            return cabinData?.count ?? 0
        }else {
            
            return airNameData?.count ?? 0
        }
       
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if tableView == table1 {
            let cell:SelectTableCell=tableView.dequeueReusableCell(withIdentifier: "tablecell1", for:indexPath ) as! SelectTableCell
            cell.lab.text=airportData?[indexPath.row].string
          
            return cell
        }else if tableView == table2{
            let cell:SelectTableCell=tableView.dequeueReusableCell(withIdentifier: "tablecell2", for:indexPath ) as! SelectTableCell
            cell.lab.text=cabinData?[indexPath.row].1
            cell.tag=cabinData?[indexPath.row].0 ?? 1
         
            return cell
        }else {
            let cell:SelectTableCell=tableView.dequeueReusableCell(withIdentifier: "tablecell3", for:indexPath ) as! SelectTableCell
            cell.lab.text=airNameData?[indexPath.row].string

         
            return cell
        }
      
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if tableView == table1 {
            
            let cell:SelectTableCell=tableView.cellForRow(at: indexPath)  as! SelectTableCell
            
            if airPortCellArr.contains(cell) {
             airPortCellArr = airPortCellArr.filter({$0 !== cell})
                airPortArr = airPortArr.filter({$0 != cell.lab.text})
                cell.img.image=UIImage(named: "筛选-圆圈")
            }else{
                airPortCellArr.append(cell)
                cell.img.image=UIImage(named: "筛选-圆圈选中")
                airPortArr.append(cell.lab.text!)
            }
                
            
        }else if tableView == table2{
            let cell:SelectTableCell=tableView.cellForRow(at: indexPath)  as! SelectTableCell
            
            if cabinCellArr.contains(cell) {
                cabinCellArr = cabinCellArr.filter({$0 !== cell})
                cabinArr = cabinArr.filter({$0 != cell.tag})
                cell.img.image=UIImage(named: "筛选-圆圈")
            }else{
                cabinCellArr.append(cell)
                cell.img.image=UIImage(named: "筛选-圆圈选中")
                cabinArr.append(cell.tag)
            }
        }else {
            let cell:SelectTableCell=tableView.cellForRow(at: indexPath)  as! SelectTableCell
            if airNameCellArr.contains(cell) {
                airNameCellArr = airNameCellArr.filter({$0 !== cell})
                airNameArr = airNameArr.filter({$0 != cell.lab.text})
                cell.img.image=UIImage(named: "筛选-圆圈")
            }else{
                airNameCellArr.append(cell)
                cell.img.image=UIImage(named: "筛选-圆圈选中")
                airNameArr.append(cell.lab.text!)
            }
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func cancel(){
        self.removeFromSuperview()
    }
    
    @objc func sure(){
        self.removeFromSuperview()
        
        if propData != nil {
            propData!(airPortArr, cabinArr, airNameArr)
        }
    }
    
    @objc func clickBtn(sender:UIButton){
        for a in btnArr {
            a.backgroundColor=UIColor.init(hexString: "f5f5f5")
        }
        sender.backgroundColor=UIColor.white
        switch sender.tag {
        case 0:
            for t in tableArr{
                t.removeFromSuperview()
            }
            box.addSubview(table1)
        case 1:
            for t in tableArr{
                t.removeFromSuperview()
            }
            box.addSubview(table2)
        case 2:
            for t in tableArr{
                t.removeFromSuperview()
            }
            box.addSubview(table3)
       
        default:
            break
        }
    }
}
